﻿Imports ACompany.OrderProcessing.Presentation

Partial Class OrderDetail
    Inherits System.Web.UI.Page
    Implements IOrderDetailView

    Private _Presenter As OrderPresenter

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        _Presenter = New OrderPresenter(Me)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            _Presenter.Display()
        End If
    End Sub

    Public Sub ErrorMessage(ByVal Message As String) Implements ACompany.OrderProcessing.Presentation.IBaseView.ErrorMessage
        If String.IsNullOrEmpty(Message) Then
            pnlError.Visible = False
        Else
            pnlError.Visible = True
        End If
        Me.litErrorMessage.Text = Replace(Message, Environment.NewLine, "<br/>")
    End Sub

    Public Sub InfoMessage(ByVal Message As String) Implements ACompany.OrderProcessing.Presentation.IBaseView.InfoMessage
        If String.IsNullOrEmpty(Message) Then
            pnlInfo.Visible = False
        Else
            pnlInfo.Visible = True
        End If
        Me.litInfoMessage.Text = Replace(Message, Environment.NewLine, "<br/>")
    End Sub

    Public Sub ViewTitle(ByVal title As String) Implements ACompany.OrderProcessing.Presentation.IBaseView.ViewTitle

    End Sub

    Public WriteOnly Property CancelOrderButtonVisible() As Boolean Implements ACompany.OrderProcessing.Presentation.IOrderDetailView.CancelOrderButtonVisible
        Set(ByVal value As Boolean)
            Me.btnCancel.Enabled = value
        End Set
    End Property

    Public WriteOnly Property Order() As ACompany.OrderProcessing.Model.Order Implements ACompany.OrderProcessing.Presentation.IOrderDetailView.Order
        Set(ByVal value As ACompany.OrderProcessing.Model.Order)

            grdOrderItems.DataSource = value.Items
            grdOrderItems.DataBind()

            Me.litDespatchName.Text = value.Customer.Name
            Me.litDespatchStreet.Text = value.DespatchAddress.Street
            Me.litDespatchCity.Text = value.DespatchAddress.City
            Me.litDespatchCounty.Text = value.DespatchAddress.County
            Me.litDespatchPostcode.Text = value.DespatchAddress.PostCode

            Me.litOrderId.Text = value.Id.ToString
            Me.litOrderDate.Text = value.OrderDate.ToLongDateString
            litOrderTime.Text = value.OrderDate.ToShortTimeString

            Session("OrderDetail") = value

        End Set
    End Property

    Public ReadOnly Property OrderId() As Guid Implements ACompany.OrderProcessing.Presentation.IOrderDetailView.OrderId
        Get
            Dim SelectedOrderId As Guid
            If String.IsNullOrEmpty(Request.QueryString("OrderId")) Then
                SelectedOrderId = Guid.Empty
            Else
                SelectedOrderId = New Guid(Request.QueryString("OrderId"))
            End If

            Return SelectedOrderId
        End Get
    End Property

    Public WriteOnly Property ShipOrderButtonVisible() As Boolean Implements ACompany.OrderProcessing.Presentation.IOrderDetailView.ShipOrderButtonVisible
        Set(ByVal value As Boolean)
            Me.btnShip.Enabled = value
        End Set
    End Property

    Protected Sub btnShip_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShip.Click
        _Presenter.Process(Session("OrderDetail"))
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        _Presenter.Cancel(Session("OrderDetail"))
    End Sub
End Class
